import CryptoJS from 'crypto-js/crypto-js'

var key_base64 = "Mch20201019162044046370000000000";
var iv_base64 = "Mch20201019162044046370000000000";
const KEY = CryptoJS.enc.Utf8.parse(key_base64);
const IV = CryptoJS.enc.Utf8.parse(iv_base64);
/**
 * AES加密 ：字符串 key iv  返回base64
 */
function AESEncrypt(word, keyStr, ivStr) {
	let key = KEY;
	let iv = IV;
	if (keyStr) {
		key = CryptoJS.enc.Utf8.parse(keyStr);
		iv = CryptoJS.enc.Utf8.parse(ivStr);
	}

	let srcs = CryptoJS.enc.Utf8.parse(word);
	var encrypted = CryptoJS.AES.encrypt(srcs, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}

/**
 * AES 解密 ：字符串 key iv  返回base64
 *
 */
function AESDecrypt(word, keyStr, ivStr) {
	let key = KEY;
	let iv = IV;

	if (keyStr) {
		key = CryptoJS.enc.Utf8.parse(keyStr);
		iv = CryptoJS.enc.Utf8.parse(ivStr);
	}

	let base64 = CryptoJS.enc.Base64.parse(word);
	let src = CryptoJS.enc.Base64.stringify(base64);

	let decrypt = CryptoJS.AES.decrypt(src, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
	return decryptedStr.toString();
}

export {
	AESEncrypt,
	AESDecrypt
}